//
//  WBEmoticonPageViewCell.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/18.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

class WBEmoticonPageViewCell: UICollectionViewCell {
    
    var buttonArray:[WBEmoticonButton] = [WBEmoticonButton]()
    
    var emoticons:[WBEmoticonModel]?{
        
        didSet{
            
            for btn in buttonArray{
                //cell复用
                btn.hidden = true
                
            }
            
            
            for (i,emoticon) in emoticons!.enumerate() {
                
                //取出按钮数组中的按钮
                let btn = buttonArray[i]
                
                //给Btn的模型属性赋值 发送通知的时候使用
                btn.emoticon = emoticon
                
                //显示键盘
                btn.hidden = false
                
                //判断是不是emoji
                if emoticon.isEmoji{
                    
                    let emoji = (emoticon.code ?? "" as NSString).emoji()
                    btn.setTitle(emoji, forState: .Normal)
                    
                    //cell复用
                    btn.setImage(nil, forState: .Normal)
                    
                }else
                {
                    
                    btn.setImage(UIImage.emoticonWithImage(emoticon), forState: .Normal)
                    //cell复用
                    btn.setTitle(nil, forState: .Normal)
                    
                }
                
                
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    // MARK: - 设置UI
    func setupUI() {
        
        //添加控件
        addChildBtn()
        contentView.addSubview(deleteBtn)
        
    }
    
    // MARK: - 循环创建按钮
    func addChildBtn() {
        
        for _ in 0..<WBEmoticonsMaxCount{
            
            //创建按钮
            let btn = WBEmoticonButton()
            
            //添加按钮点击事件
            btn.addTarget(self, action: #selector(WBEmoticonPageViewCell.btnClick(_:)), forControlEvents: .TouchUpInside)
            
            //设置文字大小
            btn.titleLabel?.font = UIFont.systemFontOfSize(30)
            
            //添加到数组中
            buttonArray.append(btn)
            
            //添加到当前控件中
            contentView.addSubview(btn)
        }
    }
    
    
    // MARK: - 设置btn的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置按钮的高度 宽度
        let btnW = (self.frame.size.width - 10)/CGFloat(WBEmoticonsMaxCol)
        let btnH = (self.frame.size.height - 20)/CGFloat(WBEmoticonsMaxRow)
        
        //设置frame
        for (index, button) in buttonArray.enumerate(){
            
            let row = CGFloat(index / WBEmoticonsMaxCol)
            let col = CGFloat(index % WBEmoticonsMaxCol)
            
            button.frame = CGRect(x: col * btnW + 5, y: row * btnH, width: btnW, height: btnH)
        }
        
        deleteBtn.frame = CGRect(x: self.frame.size.width - 5 - btnW, y: 2 * btnH, width: btnW, height: btnH)
    }
    
    
    // MARK: - 懒加载删除按钮
    private lazy var deleteBtn:UIButton = {
        
        let btn = UIButton()
        
        //添加按钮点击事件
        btn.addTarget(self, action: #selector(WBEmoticonPageViewCell.delebtnClick), forControlEvents: .TouchUpInside)
        
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        
        return btn
    }()
    
    // MARK: - 删除按钮点击事件
    func delebtnClick() {
        
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonKeyBoardDeleteBtnNotification, object: nil)
        
    }
    
    // MARK: - 表情按钮点击事件
    func btnClick(button:WBEmoticonButton) {
        
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonKeyBoardBtnNotification, object: button.emoticon)
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
